package com.letv.whatslive.model.convert;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.string.StringUtils;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.User;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by gaoshan on 15-7-7.
 */
public class UserConvert {

    public static DBObject castUserToDBObject(User user) {
        DBObject dbo = new BasicDBObject();
        if (user.getUserId() != null) {
            dbo.put("_id", user.getUserId());
        }
        dbo.put("userName", ObjUtils.ifNull(user.getUserName(), ""));
        dbo.put("nickName", ObjUtils.ifNull(user.getNickName(), ""));
        dbo.put("userType", ObjUtils.ifNull(user.getUserType(), 0));
        dbo.put("sex", ObjUtils.ifNull(user.getSex(), 0));
        dbo.put("mobile", ObjUtils.ifNull(user.getMobile(), ""));
        dbo.put("phone", ObjUtils.ifNull(user.getPhone(), ""));
        dbo.put("email", ObjUtils.ifNull(user.getEmail(), ""));
        dbo.put("address", ObjUtils.ifNull(user.getAddress(), ""));
        dbo.put("picture", ObjUtils.ifNull(user.getPicture(), ""));
        dbo.put("userStatus", ObjUtils.ifNull(user.getUserStatus(), 1));
        dbo.put("thirdId", ObjUtils.ifNull(user.getThirdId(), ""));
        dbo.put("createTime", ObjUtils.toLong(user.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(user.getUpdateTime(), System.currentTimeMillis()));
        dbo.put("lastLoginTime", ObjUtils.toLong(user.getLastLoginTime(), System.currentTimeMillis()));
        dbo.put("accessToken", ObjUtils.toString(user.getAccessToken()));
        dbo.put("role", ObjUtils.toInteger(user.getRole(), 0));
        dbo.put("level", ObjUtils.toInteger(user.getLevel(), 0));
        dbo.put("introduce", ObjUtils.toString(user.getIntroduce(), ""));
        dbo.put("likeNum", ObjUtils.toLong(user.getLikeNum(), 0l));
        dbo.put("sinaBind", ObjUtils.toInteger(user.getSinaBind(), 0));
        dbo.put("qqBind", ObjUtils.toInteger(user.getQqBind(), 0));
        dbo.put("weixinBind", ObjUtils.toInteger(user.getWeixinBind(), 0));
        dbo.put("sinaId", ObjUtils.toString(user.getSinaId(), ""));
        dbo.put("qqId", ObjUtils.toString(user.getQqId(), ""));
        dbo.put("weixinId", ObjUtils.toString(user.getWeixinId(), ""));
        dbo.put("isRobot", ObjUtils.toInteger(user.getIsRobot(), 1));
        dbo.put("isAllowCommentEndDate", ObjUtils.toLong(user.getIsAllowCommentEndDate(), 0L));
        dbo.put("isAllowLiveEndDate", ObjUtils.toLong(user.getIsAllowLiveEndDate(), 0L));
        dbo.put("isAllowComment", ObjUtils.toInteger(user.getIsAllowComment(), 1));
        dbo.put("isAllowLive", ObjUtils.toInteger(user.getIsAllowLive(), 1));
        if (user.getDevIdList() != null && user.getDevIdList().size() > 0) {
            dbo.put("devIdList", user.getDevIdList());
        } else {
            dbo.put("devIdList", new ArrayList<Long>());
        }
        if (user.getTags() != null) {
            dbo.put("tags", user.getTags());
        }
        dbo.put("sso_tk", ObjUtils.toString(user.getSsoTk()));
        dbo.put("recommend", ObjUtils.toString(user.getRecommend()));
        dbo.put("broadCastNum", ObjUtils.toLong(user.getBroadCastNum(), 0L));
        dbo.put("following", ObjUtils.toLong(user.getFollowing(), 0L));
        dbo.put("follower", ObjUtils.toLong(user.getFollower(), 0L));
        dbo.put("planProgramNum", ObjUtils.toLong(user.getPlanProgramNum(), 0L));
        dbo.put("isHasRobot", ObjUtils.toInteger(user.getIsHasRobot(), 1));
        if (user.getLogo() != null) {
            dbo.put("logo", user.getLogo());
        }

        if (user.getBackgroundPic() != null) {
            dbo.put("backgroundPic", user.getBackgroundPic());
        }
        dbo.put("userMessageUpdateTime", ObjUtils.toLong(user.getUserMessageUpdateTime()));
        dbo.put("userMessageQueryTime", ObjUtils.toLong(user.getUserMessageQueryTime()));
        if (user.getLocationToShow() != null && user.getLocationToShow().size() > 0) {
            dbo.put("locationToShow", user.getLocationToShow());
        } else {
            dbo.put("locationToShow", new ArrayList<Integer>(Arrays.asList(1,2,3)));
        }

        dbo.put("appId", ObjUtils.toString(user.getAppId(), "10000"));
        dbo.put("isContracted", ObjUtils.toInteger(user.getIsContracted(), 0));
//        dbo.put("realNamePhoneNo", ObjUtils.toString(user.getRealNamePhoneNo(), ""));
//        dbo.put("countryCodeId", ObjUtils.toLong(user.getCountryCodeId(),-1L));
        if(!StringUtils.isBlank(user.getRealNamePhoneNo()))
            dbo.put("realNamePhoneNo", ObjUtils.toString(user.getRealNamePhoneNo()));
        if(user.getCountryCodeId()!=null)
            dbo.put("countryCodeId", ObjUtils.toLong(user.getCountryCodeId()));

        if(user.getOpenid()!=null)
            dbo.put("openid", ObjUtils.toString(user.getOpenid()));
        if(user.getWeiDianStatus()!=null)
            dbo.put("weiDianStatus", ObjUtils.toInteger(user.getWeiDianStatus()));

        dbo.put("idCardNo", ObjUtils.toString(user.getIdCardNo(),""));
        dbo.put("idCardUpPic", ObjUtils.toString(user.getIdCardUpPic(),""));
        dbo.put("idCardDownPic", ObjUtils.toString(user.getIdCardDownPic(),""));
        dbo.put("realName", ObjUtils.toString(user.getRealName(),""));
        dbo.put("identityAuditStatus", ObjUtils.toInteger(user.getIdentityAuditStatus(), 0));
        dbo.put("bankCardNo", ObjUtils.toString(user.getBankCardNo(), ""));
        return dbo;
    }

    public static User castDBObjectToUser(DBObject dbObject) {
        if (dbObject != null) {
            User user = new User();
            user.setUserId(ObjUtils.toLong(dbObject.get("_id")));
            user.setUserName(ObjUtils.toString(dbObject.get("userName"), ""));
            user.setNickName(ObjUtils.toString(dbObject.get("nickName")));
            user.setSex(ObjUtils.toInteger(dbObject.get("sex")));
            user.setPicture(ObjUtils.toString(dbObject.get("picture")));
            user.setUserType(ObjUtils.toInteger(dbObject.get("userType")));


            user.setRole(ObjUtils.toInteger(dbObject.get("role")));
            user.setLevel(ObjUtils.toInteger(dbObject.get("level"), 0));
            user.setLikeNum(ObjUtils.toLong(dbObject.get("likeNum"), 0L));

            user.setIntroduce(ObjUtils.toString(dbObject.get("introduce")));
            user.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            user.setLastLoginTime(ObjUtils.toLong(dbObject.get("lastLoginTime")));
            user.setUserStatus(ObjUtils.toInteger(dbObject.get("userStatus"), 1));
            user.setIsAllowCommentEndDate(ObjUtils.toLong(dbObject.get("isAllowCommentEndDate"), 0L));
            user.setIsAllowComment(ObjUtils.toInteger(dbObject.get("isAllowComment"), 1));
            user.setIsAllowLiveEndDate(ObjUtils.toLong(dbObject.get("isAllowLiveEndDate"), 0L));
            user.setIsAllowLive(ObjUtils.toInteger(dbObject.get("isAllowLive"), 1));
            user.setBroadCastNum(ObjUtils.toLong(dbObject.get("broadCastNum"), 0L));
            user.setSinaBind(ObjUtils.toInteger(dbObject.get("sinaBind"), 0));
            user.setQqBind(ObjUtils.toInteger(dbObject.get("qqBind"), 0));
            user.setWeixinBind(ObjUtils.toInteger(dbObject.get("weixinBind"), 0));
            user.setSinaId(ObjUtils.toString(dbObject.get("sinaId"), ""));
            user.setQqId(ObjUtils.toString(dbObject.get("qqId"), ""));
            user.setWeixinId(ObjUtils.toString(dbObject.get("weixinId"), ""));
            user.setIsRobot(ObjUtils.toInteger(dbObject.get("isRobot"), 1));
            user.setIsHasRobot(ObjUtils.toInteger(dbObject.get("isHasRobot"), 1));
            if (dbObject.get("devIdList") != null) {
                BasicDBList dbList = (BasicDBList) (dbObject.get("devIdList"));
                if (dbList.size() > 0) {
                    List<String> devList = new ArrayList<String>();
                    for (int i = 0; i < dbList.size(); i++) {
                        devList.add(String.valueOf(dbList.get(i)));
                    }
                    user.setDevIdList(devList);
                }
            }

            if (dbObject.get("tags") != null) {
                if (dbObject.get("tags") instanceof BasicDBList) {
                    BasicDBList dbList = (BasicDBList) (dbObject.get("tags"));
                    if (dbList.size() > 0) {
                        List<Map<String, Object>> tags = Lists.newArrayList();
                        for (int i = 0; i < dbList.size(); i++) {
                            if (dbList.get(i) instanceof Map) {
                                tags.add((Map<String, Object>) (dbList.get(i)));
                            } else {
                                throw new ClassCastException("tags can not cast to Map!");
                            }
                        }
                        user.setTags(tags);
                    }
                } else if (dbObject.get("tags") instanceof BasicDBObject && ((BasicDBObject) dbObject.get("tags")).size() > 0) {
                    List<Map<String, Object>> tags = Lists.newArrayList();
                    Map<String, Object> tag = ((BasicDBObject) (dbObject.get("tags"))).toMap();
                    tags.add(tag);
                    user.setTags(tags);
                }
            }

            user.setAccessToken(ObjUtils.toString(dbObject.get("accessToken"), ""));
            user.setSsoTk(ObjUtils.toString(dbObject.get("sso_tk"), ""));
            user.setRecommend(ObjUtils.toString(dbObject.get("recommend"), ""));
            user.setFollowing(ObjUtils.toLong(dbObject.get("following"), 0L));
            user.setFollower(ObjUtils.toLong(dbObject.get("follower"), 0L));
            user.setPlanProgramNum(ObjUtils.toLong(dbObject.get("planProgramNum"), 0L));
            user.setLogo(ObjUtils.toString(dbObject.get("logo")));
            user.setBackgroundPic(ObjUtils.toString(dbObject.get("backgroundPic")));
            user.setUserMessageUpdateTime(ObjectUtils.toLong(dbObject.get("userMessageUpdateTime")));
            user.setUserMessageQueryTime(ObjectUtils.toLong(dbObject.get("userMessageQueryTime")));
            if (dbObject.get("locationToShow") != null) {
                BasicDBList dbList = (BasicDBList) (dbObject.get("locationToShow"));
                if (dbList.size() > 0) {
                    List<Integer> locationToShow = new ArrayList<Integer>();
                    for (int i = 0; i < dbList.size(); i++) {
                        locationToShow.add(ObjUtils.toInteger(dbList.get(i)));
                    }
                    user.setLocationToShow(locationToShow);
                }
            }
            else{
                user.setLocationToShow(new ArrayList<Integer>(Arrays.asList(1,2,3)));
            }
            user.setAppId(ObjectUtils.toString(dbObject.get("appId"), "10000"));
            user.setIsContracted(ObjUtils.toInteger(dbObject.get("isContracted"), 0));
            if(!StringUtils.isBlank(ObjectUtils.toString(dbObject.get("realNamePhoneNo"))))
                user.setRealNamePhoneNo(ObjectUtils.toString(dbObject.get("realNamePhoneNo")));
            if(dbObject.get("countryCodeId")!=null)
                user.setCountryCodeId(ObjUtils.toLong(dbObject.get("countryCodeId")));

            if(!StringUtils.isBlank(ObjectUtils.toString(dbObject.get("openid"))))
                user.setOpenid(ObjectUtils.toString(dbObject.get("openid")));
            if(dbObject.get("weiDianStatus")!=null)
                user.setWeiDianStatus(ObjUtils.toInteger(dbObject.get("weiDianStatus")));

            if(dbObject.get("idCardNo")!=null)
                user.setIdCardNo(ObjUtils.toString(dbObject.get("idCardNo")));
            if(dbObject.get("idCardUpPic")!=null)
                user.setIdCardUpPic(ObjUtils.toString(dbObject.get("idCardUpPic")));
            if(dbObject.get("idCardDownPic")!=null)
                user.setIdCardDownPic(ObjUtils.toString(dbObject.get("idCardDownPic")));
            if(dbObject.get("realName")!=null)
                user.setRealName(ObjUtils.toString(dbObject.get("realName")));
            user.setIdentityAuditStatus(ObjUtils.toInteger(dbObject.get("identityAuditStatus"),0));
            if(dbObject.get("bankCardNo")!=null)
                user.setBankCardNo(ObjUtils.toString(dbObject.get("bankCardNo")));

            return user;
        } else {
            return null;
        }

    }
}
